package com.innerchic.mapper.app;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.innerchic.annotations.base.FragmentSetting;
import com.innerchic.mapper.app.base.Action;
import com.innerchic.mapper.app.base.EventListener;
import com.innerchic.mapper.app.base.Lifecycle;
import com.innerchic.mapper.resfind.NotFind;
import com.innerchic.mapper.resfind.ResFind;
import com.innerchic.sponsor.Sponsor;

import java.util.Objects;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：young
 * 日    期：2021/11/1 17:53
 * 包    名：com.innerchic.mapper.app
 * 描    述：超级Fragment
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public abstract class SuperFragment extends Fragment implements Lifecycle {

    @NotFind
    private final FragmentSetting supperTag = this.getClass().getAnnotation(FragmentSetting.class);

    private View supperView;

    public View getSupperView() {
        return supperView;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (supperTag != null) {
            beforeInit();
            supperView = inflater.inflate(ResFind.getInstance().findLayout(supperTag.layout()),
                    container, false);
            init();
            create();
        } else {
            loadError();
        }
        return supperView;
    }

    @Override
    public void onStart() {
        super.onStart();
        start();
    }

    @Override
    public void onStop() {
        super.onStop();
        stop();
    }

    @Override
    public void onResume() {
        super.onResume();
        resume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //判断是否需要解绑定监听器
        if (supperTag != null && supperTag.observer()) {
            Sponsor.getDefault().unregister(this);
        }
        destroy();
    }

    @Override
    public void beforeInit() {
    }

    @Override
    public void init() {
        //初始化页面参数
        if (supperTag != null && supperTag.bind().equals(FragmentSetting.ResBind)) {
            ResFind.getInstance().bind(supperView, this);
        }
        //判断是否需要绑定监听器
        if (supperTag != null && supperTag.observer()) {
            Sponsor.getDefault().register(this);
        }
    }

    @Override
    public void start() {
    }

    @Override
    public void resume() {

    }

    @Override
    public void stop() {

    }

    public String getTitle() {
        return this.getClass().getSimpleName();
    }

    @Override
    public void loadError() {
        getSupperActivity().showToast("布局Fragment加载失败,找不到入口配置");
    }

    /**
     * 获取主进程Handler
     *
     * @return Handler
     */
    protected Handler getMainHandler() {
        return Objects.requireNonNull(getSupperActivity()).getMainHandler();
    }

    /**
     * 获取基带Activity
     *
     * @return SupperActivity
     */
    protected SuperActivity getSupperActivity() {
        return (SuperActivity) getActivity();
    }

    /**
     * 显示SnackBar
     *
     * @param view    view
     * @param actions 动作
     */
    protected void showSnack(View view, String msg, Action... actions) {
        getSupperActivity().showSnack(view, msg, actions);
    }

    protected void showToast(String toast) {
        if (toast == null) return;
        getSupperActivity().showToast(toast);
    }

    private EventListener eventListener = new EventListener() {
    };

    public void setEventListener(EventListener eventListener) {
        this.eventListener = eventListener;
    }

    public EventListener getEventListener() {
        return eventListener;
    }


}
